package com.ruoyi.common.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ruoyi.common.dto.SearchDTO;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.QueryWrapperUtils;
import com.ruoyi.common.vo.PageVO;
import com.google.common.collect.Maps;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;

public interface ISuperService<T> extends IService<T> {
    Map<String, String> PROPERTY_COLUMN_MAP = Maps.newHashMap();

    default int deleteById(Serializable id) {
        return this.getBaseMapper().deleteById(id);
    }

    default int delete(Wrapper<T> wrapper) {
        return this.getBaseMapper().delete(wrapper);
    }

    default PageVO<T> page(SearchDTO searchDTO, LambdaQueryWrapper<T> queryWrapper) {
        Page<T> searchPage = this.packSearchPage(searchDTO);
        Page<T> page = (Page)this.page((IPage)searchPage, queryWrapper);
        return new PageVO(page);
    }

    default PageVO<T> page(SearchDTO searchDTO, SFunction<T, ?>... fieldNames) {
        LambdaQueryWrapper<T> queryWrapper = this.packQueryWrapper(searchDTO, fieldNames);
        return this.page(searchDTO, queryWrapper);
    }

    default LambdaQueryWrapper<T> packQueryWrapper(SearchDTO searchDTO, SFunction<T, ?>... fieldNames) {
        LambdaQueryWrapper<T> queryWrapper = QueryWrapperUtils.wrapperLike(new LambdaQueryWrapper(), searchDTO.getSearchKey(), fieldNames);
        return queryWrapper;
    }

    default Page<T> packSearchPage(SearchDTO searchDTO) {
        Page<T> searchPage = new Page();
        if (searchDTO.getCurrent() != null && searchDTO.getCurrent() > 0) {
            searchPage.setCurrent((long)searchDTO.getCurrent());
        } else {
            searchPage.setCurrent(1L);
        }

        Integer current = searchDTO.getCurrent();
        if (current == null) {
            current = 1;
        }

        Integer size = searchDTO.getSize();
        if (size == null || size <= 0) {
            size = 10;
        }

        if (size > 500) {
            size = 500;
        }

        searchPage.setSize((long)size);
        searchPage.setCurrent((long)current);
        return searchPage;
    }

    default List<String> convert(String idStr) {
        if (Strings.isBlank(idStr)) {
            throw new GlobalException("id不允许为空");
        } else {
            String[] idStrArr = StringUtils.split(idStr, "-");
            return (List)Arrays.stream(idStrArr).map((id) -> id).collect(Collectors.toList());
        }
    }

    default T getEntityByField(SFunction<T, ?> field, Object val) {
        LambdaUpdateWrapper<T> query = new LambdaUpdateWrapper();
        query.eq(field, val);
        List<T> list = this.list(query);
        return (T)(list != null && list.size() != 0 ? list.get(0) : null);
    }

    default List<T> getEntitiesByField(SFunction<T, ?> field, Object val) {
        LambdaUpdateWrapper<T> query = new LambdaUpdateWrapper();
        query.eq(field, val);
        List<T> list = this.list(query);
        return list;
    }
}

